﻿using System.ComponentModel;
using System.Windows;
using Mari.SmartHotel.Controls;
using Microsoft.Services.Clab.UI.Mvvmc;
using Microsoft.Services.Clab.UI.Mvvmc.Presentation;
using Microsoft.Windows.Controls.Ribbon;

namespace Mari.SmartHotel.Shell
{
    public partial class ShellView : RibbonWindow, IView
    {
        public ShellView()
        {
            InitializeComponent();

            this.Initialized += (s, e) => WpfViewHelper.OnControlInitialized(s as IView, (bool)DesignerProperties.GetIsInDesignMode(this));
            this.Loaded += (s, e) => WpfViewHelper.OnControlLoaded(s as IView);
        }

        protected override void OnInitialized(System.EventArgs e)
        {
            base.OnInitialized(e);

            ShellPanesService.TaskPaneContentControl = taskPane;
            ShellPanesService.HelpPaneContentControl = helpPane;
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            WpfViewHelper.OnContentChanged(this, oldContent, newContent);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            WpfViewHelper.UpdatePropertyMapping(this, e.Property);
        }

        #region IView Members

        public void Initialize(IViewModel viewModel)
        {
            WpfViewHelper.InitializeView(this, viewModel);
        }

        #region ViewModel Property
        public IViewModel ViewModel
        {
            get { return this.viewModel; }
            set { if (this.viewModel != value) { this.viewModel = value; this.OnPropertyChanged(new PropertyChangedEventArgs("ViewModel")); } }
        }
        private IViewModel viewModel;
        #endregion

        #region State Property
        public ViewState State
        {
            get { return this.state; }
            set { if (this.state != value) { this.state = value; this.OnPropertyChanged(new PropertyChangedEventArgs("State")); } }
        }
        private ViewState state;
        #endregion

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, args);
            }
        }

        #endregion

        #region Dispose pattern

        ~ShellView()
        {
            this.Dispose(false);
        }

        public void Dispose()
        {
            WpfViewHelper.Dispose(this, v => v.Dispose(true));
        }

        protected virtual void Dispose(bool disposing)
        {
        }

        #endregion
    }
}